from typing import Annotated

from langchain_core.tools import tool
from langchain_experimental.utilities import PythonREPL
from langdev_helper.llm.qwen import llm


repl = PythonREPL()


@tool
def python_repl(
    code: Annotated[str, "The python code to execute to generate your chart."],
):
    """Use this to execute python code. If you want to see the output of a value,
    you should print it out with `print(...)`. This is visible to the user."""
    try:
        result = repl.run(code)
    except BaseException as e:
        return f"Failed to execute. Error: {repr(e)}"
    result_str = f"Successfully executed:\n```python\n{code}\n```\nStdout: {result}"
    return (
        result_str + "\n\nIf you have completed all tasks, respond with FINAL ANSWER."
    )

code_ex = '''
ret = 7 + 3
print(ret)
'''

# r = python_repl(code_ex)
# print(r)


llm_ret = '''
```python
result = 5 + 6 + 7
print(result)
```
'''



llm_ret = llm.invoke("使用py来计算从1加到100")
# print(text.content)
llm_ret = llm_ret.content

code_ = llm_ret.split("```python")[1].split("```")[0]
print(python_repl(code_))
